<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>[Tiny]Klass Unit Tests</title>
<link href="qunit/qunit-1.10.0.css" media="all" rel="stylesheet" type="text/css">
</head>

<body>

<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script type="text/javascript" src="qunit/qunit-1.10.0.js"></script>
<script type="text/javascript">
(function ( window, undefined ) {

// Can record messages sent to console.log, so we can check them
if ( window.console ) {
	var recordedLogs = [],
		showInConsole,
		originalLog = console.log;
		recordLogs = function( ) {
			recordedLogs.push( Array.prototype.slice.call( arguments ) );
			if ( showInConsole ) originalLog.apply( window.console, arguments );
		};
	window.console.startRecordingLogs = function( _showInConsole ) {
		window.console.log = recordLogs;
		showInConsole = _showInConsole;
	};
	window.console.getRecordedLogs = function( reset ) {
		if ( reset ) {
			var logs = recordedLogs;
			recordedLogs = [];
			return logs;
		}
		else return Array.prototype.slice.call( recordedLogs );
		
	};
	window.console.stopRecordingLogs = function( ) {
		window.console.log = originalLog;
		return window.console.getRecordedLogs( true );
	};
}

// Checks that 2 klasses are equals: all fields must be strictly equals, uses recursion
window.klassEqual = function(obj1, obj2) {
	if(obj1 !== obj2) {
		for(f in obj1) {
			var f1 = obj1[f], f2 = obj2[f],
				t1 = undefined, t2 = undefined;
			if(f1 !== f2) {
				if ( f1._klass && f1._klass.originalMethod ) t1 = f1._klass.originalMethod;
				if ( f2._klass && f2._klass.originalMethod ) t2 = f2._klass.originalMethod;
				if(t1 || t2) {
					if(f1 === f2) continue;
				}
				else if(typeof(f1) === "object") {
					if(klassEqual(f1, f2)) continue;
				}
				else return false
			}
		}
		
		for(f in obj2) if(!(f in obj1)) return false;
		
		if(obj1.prototype) {
			if(!klassEqual(obj1.prototype, obj2.prototype)) return false;
		}
	}
	return true;
};

})( window );
</script>
<script type="text/javascript" src="jquery/jquery-1.8.3.min.js"></script>

<script type="text/javascript">
(function ( $, window, document, undefined ) {

	// Configuration:
	var folder = "../",
		types = ["klass", "tinyklass"],
		typesPrefixes = ["jquery.klass", "jquery.tinyklass"],
		load = function() {
			var TL = TestsLoaded;
				
			loadScript("private");
			loadScript("traits");
			loadScript("proxy");
			loadScript("namespace");
			
			loadTest("klass");
			if(TL.scripts["private"]) loadTest("private");
			if(TL.scripts["traits"]) loadTest("traits");
			if(TL.scripts["proxy"]) loadTest("proxy");
			if(TL.scripts["namespace"]) loadTest("namespace");
			
			loadTest("wiki_klass");
			if(TL.scripts["private"]) loadTest("wiki_privateproperties");
			if(TL.scripts["traits"]) loadTest("wiki_traits");
			if(TL.scripts["proxy"]) loadTest("wiki_proxy");
			if(TL.scripts["namespace"]) loadTest("wiki_namespace");
			loadTest("wiki_advancedusage");
		};
	
	var titles = ["Klass Unit Test - jquery-klass-plugin", "Tinyklass Unit Test - jquery-tinyklass-plugin"],
		type = -1,
		getScriptPath = function(name) {
			if(name === "klass") name = "";
			else if(name !== "") name = "." + name;
			return folder + typesPrefixes[type] + name + ".js";
		},
		getTestPath = function(name) {
			return "test_" + name + ".js";
		},
		loadScript = function(name) {
			TestsLoaded.scripts[name] = true;
			document.write('<script type="text/javascript" src="' + getScriptPath(name) + '"><\/script>');
		},
		loadTest = function(name) {
			TestsLoaded.tests[name] = true;
			document.write('<script type="text/javascript" src="' + getTestPath(name) + '"><\/script>');
		},
		message = function(title, value, errorMessage) {
			module("Message");
			test(title, function() {
				ok(value, errorMessage);
			});
		},
		TestsLoaded = window.TestsLoaded = {
			scripts: {},
			tests: {}
		},
		klassFound = function() {
			delete _klassLookup.tryNext;
			delete TestsLoaded.scripts["klass"];
			_klassLookup.type = types[type];
			$("head title").text(titles[type]);
			load();
		},
		klassNotFound = function() {
			delete _klassLookup.tryNext;
			type = -1;
			$("head title").text("No klass file found !");
			message("No klass file found !", false, "Neither jquery.klass.js nor jquery.tinyklass.js could be found !");
		},
		_klassLookup = window._klassLookup = {
			tryNext: function() {
				// Check if klass has been found:
				if($.klass) {
					console.log("Klass found at: " + getScriptPath("klass"));
					klassFound();
					return;
				}
				
				// Increment search:
				type++;
				if(type === typesPrefixes.length) {
					console.log("Klass not found.");
					klassNotFound();
					return;
				}
				
				// Try to load next:
				loadScript("klass");
				document.write('<script type="text/javascript">window._klassLookup.tryNext();<\/script>');
			}
		};
	
	_klassLookup.tryNext();
})( jQuery, window, window.document );
</script>

</body>
</html>